// var o;
// exports.LatticeConfig = void 0;
// var AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
// var AL_GAME_HUA_GameData = require("./AL_GAME_HUA_GameData");
// var AL_GAME_HUA_ConfigHandler = require("./AL_GAME_HUA_ConfigHandler");
// var AL_GAME_HUA_GameConst = require("./AL_GAME_HUA_GameConst");
// var AL_GAME_HUA_LevelConfig = require("./AL_GAME_HUA_LevelConfig");
// var f = {
//     id: 1006,
//     row: 4,
//     col: 3,
//     list: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
//     difficult: 1,
//     displaceBase: [3, 3],
//     zhixing_cengji_weiyi_caozuo: [3, 3],
//     displaceMax: [50, 50],
//     increaseBase: [0.08, 0.08],
//     increaseLayer: [0.13, 0.13],
//     reduceBase: [0.08, 0.08],
//     reduceLayer: [0.13, 0.13],
//     volatilityMultiple: 1,
//     type: 1
// };
// var d = [6, 3, 1];
// var h = [0, 0, 1];
// var _ = [1, 0, 0];
// var P = [5, 3, 2];
// var g = [5, 3, 2];
// var y = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.configs = [];
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.jiazai_peizhi = function () {
//         return __awaiter(this, void 0, Promise, function () {
//             var e;
//             return __generator(this, function (t) {
//                 switch (t.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_txt_peizhi("LatticeGenerate")];
//                     case 1:
//                         return (e = t.sent()) ? ((this.configs = e), [2, e]) : [2, null];
//                 }
//             });
//         });
//     };
//     t.prototype.jiazai_tiaozhan_peizhi = function () {
//         return __awaiter(this, void 0, Promise, function () {
//             var e;
//             return __generator(this, function (t) {
//                 switch (t.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_txt_peizhi("ChallengeGenerate")];
//                     case 1:
//                         return (e = t.sent()) ? ((this.challengeConfigs = e), [2, e]) : [2, null];
//                 }
//             });
//         });
//     };
//     t.prototype.shengcheng_guanqia_peizhi_by_level_number = function (e) {
//         var t;
//         var n = d.slice();
//         e % 9 == 0 ? (t = 3) : (n.pop(), (t = this.huoqu_weight_suoyin(n) + 1));
//         var o = this.configs
//             .filter(function (e) {
//                 return e.difficult == t;
//             })
//             .huoqu_shuzu_zhong_suiji_yige_yuansu();
//         return this.shengcheng_guanqia_peizhi_by_level_number_peizhi(e, o);
//     };
//     t.prototype.shengcheng_tiaozhan_guanqia_peizhi_by_level_number = function (e) {
//         var t = this.challengeConfigs;
//         var n = t.level1;
//         var o = t.level2;
//         var i = n.huoqu_shuzu_zhong_suiji_yige_yuansu();
//         var r = o.huoqu_shuzu_zhong_suiji_yige_yuansu();
//         return [this.shengcheng_guanqia_peizhi_by_level_number_peizhi(e, i), this.shengcheng_guanqia_peizhi_by_level_number_peizhi(e, r)];
//     };
//     t.prototype.shengcheng_guanqia_peizhi_by_level_number_peizhi = function (e, t) {
//         var n = t.id;
//         var o = t.latticeTotal;
//         var i = t.type;
//         var r = t.latticePer;
//         var a = t.elementTypeCount;
//         var s = t.empty;
//         var c = t.timePer;
//         var l = t.null0;
//         var u = t.null1;
//         var p = t.null2;
//         var f = t.null3;
//         var d = t.single;
//         var y = t.lock;
//         var m = t.difficult;
//         var v = {};
//         v.ID = e;
//         v.Lattce = o;
//         var D = this.huoqu_weight_suoyin(h);
//         var L = a.replace(/\(/g, "").replace(/\)/g, "").split(",");
//         var b = Math.get_random_int(Number(L[2 * D]), Number(L[2 * D + 1]) + 1);
//         v.Type = b;
//         v.Number = 3 * b;
//         var C = this.huoqu_weight_suoyin(_);
//         var H = s.replace(/\(/g, "").replace(/\)/g, "").split(",");
//         var I = 0 == C ? Math.get_random_int(Number(H[0]), Number(H[1]) + 1) : Number(H[C + 1]);
//         v.Empty = I;
//         v.Null0 =
//             "number" == typeof l
//                 ? [Number(l), Number(l), Number(l)]
//                 : [Number(l.split(",")[0]), Number(l.split(",")[1]), Number(l.split(",")[2])];
//         v.Null1 =
//             "number" == typeof u
//                 ? [Number(u), Number(u), Number(u)]
//                 : [Number(u.split(",")[0]), Number(u.split(",")[1]), Number(u.split(",")[2])];
//         v.Null2 =
//             "number" == typeof p
//                 ? [Number(p), Number(p), Number(p)]
//                 : [Number(p.split(",")[0]), Number(p.split(",")[1]), Number(p.split(",")[2])];
//         v.Null3 =
//             "number" == typeof f
//                 ? [Number(f), Number(f), Number(f)]
//                 : [Number(f.split(",")[0]), Number(f.split(",")[1]), Number(f.split(",")[2])];
//         var S = this.huoqu_weight_suoyin(P);
//         if (0 == Number(d)) {
//             v.Specialgrid1 = 0;
//         } else {
//             {
//                 var w = d.split(",");
//                 v.Specialgrid1 = Number(w[S]);
//             }
//         }
//         var Y = this.huoqu_weight_suoyin(g);
//         if (0 == Number(y)) {
//             v.Specialgrid2 = 0;
//         } else {
//             {
//                 var E = y.split(",");
//                 v.Specialgrid2 = Number(E[Y]);
//             }
//         }
//         var A = c.replace(/\(/g, "").replace(/\)/g, "").split(",");
//         var T = ((3 * b) / r) * Math.get_random_int(Number(A[2 * C]), Number(A[2 * C + 1]) + 1);
//         v.Time = Math.round(T);
//         v.Storey = v.Specialgrid1 > 0 ? 3 : 0;
//         v.shelf_type = Number(n);
//         v.latticeType = i;
//         v.difficult = m;
//         return v;
//     };
//     t.prototype.generate = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         e = [];
//                         return [4, AL_GAME_HUA_LevelConfig.LevelConfig.getInstance().jiazai_peizhi()];
//                     case 1:
//                         t = a.sent();
//                         return [4, this.jiazai_peizhi()];
//                     case 2:
//                         for (
//                             n = a.sent(),
//                                 o = function (o) {
//                                     if (o < 10) {
//                                         e.push(t[o]);
//                                         return "continue";
//                                     }
//                                     if (Math.random() > 0.5) {
//                                         e.push(t[o]);
//                                         return "continue";
//                                     }
//                                     var r;
//                                     var a = t[o];
//                                     var s = a.ID;
//                                     var c = a.shelf_type;
//                                     var l = a.Null0;
//                                     var u = a.Null1;
//                                     var p = a.Null2;
//                                     var f = a.Null3;
//                                     var d = a.Lattce;
//                                     r = 1 == c ? 1e3 + d / 3 : c;
//                                     var h = i.shengcheng_guanqia_peizhi_by_level_number_peizhi(
//                                         s,
//                                         n.filter(function (e) {
//                                             return e.id == r;
//                                         })[0]
//                                     );
//                                     h.Null0 = l;
//                                     h.Null1 = u;
//                                     h.Null2 = p;
//                                     h.Null3 = f;
//                                     e.push(h);
//                                 },
//                                 i = this,
//                                 r = 0;
//                             r < t.length;
//                             r++
//                         ) {
//                             o(r);
//                         }
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.shengcheng_tiaozhan_guanqia = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var n;
//             return __generator(this, function () {
//                 e = [];
//                 for (t = 0; t < 200; t++) {
//                     n = this.shengcheng_tiaozhan_guanqia_peizhi_by_level_number(t);
//                     e.push(n);
//                 }
//                 console.log(JSON.stringify(e));
//                 return [2];
//             });
//         });
//     };
//     t.prototype.shengcheng_teshu_guanqia = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             return __generator(this, function () {
//                 return [2];
//             });
//         });
//     };
//     t.prototype.huoqu_weight_suoyin = function (e) {
//         var t = 0;
//         e.forEach(function (e) {
//             t += e;
//         });
//         var n = Math.random() * t;
//         var o = 0;
//         for (var i = 0; i < e.length; i++) {
//             if (n < (o += e[i])) {
//                 return i;
//             }
//         }
//         return e.length - 1;
//     };
//     return t;
// })(AL_GAME_HUA_ConfigHandler.ConfigHandler);
// var m = [
//     [60, 40, 30, 10, 5],
//     [30, 40, 60, 10, 5],
//     [10, 30, 60, 10, 5]
// ];
// var v = (function (e) {
//     function t() {
//         var t = e.call(this) || this;
//         t.levelGenerator = null;
//         t.configMap = {};
//         t.challengeConfigs = {};
//         t.customLvConf = {};
//         t.levelGenerator = new y();
//         var n = AL_GAME_HUA_GameConst.GameConst.getInstance();
//         if (n.lv2hard_enabled) {
//             var o = n.lv2confs[1];
//             f.displaceBase = [o[0], o[1]];
//             f.zhixing_cengji_weiyi_caozuo = [o[2], o[3]];
//             f.displaceMax = [o[4], o[5]];
//         }
//         return t;
//     }
//     __extends(t, e);
//     t.getInstance = function () {
//         return this.instance || (this.instance = new t());
//     };
//     t.prototype.destroy = function () {
//         t.instance = null;
//     };
//     t.prototype.jiazai_peizhi = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var s;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         return [4, this.yibu_jiazai_txt_peizhi("Lattice")];
//                     case 1:
//                         if (!(e = a.sent())) {
//                             return [3, 4];
//                         }
//                         for (t = 0, n = e; t < n.length; t++) {
//                             o = n[t];
//                             i = o.list;
//                             r = String(i)
//                                 .split("")
//                                 .map(function (e) {
//                                     return parseInt(e);
//                                 });
//                             s = JSON.parse(JSON.stringify(o));
//                             this.configMap[o.id] = s;
//                             s.list = r.slice();
//                             s = JSON.parse(JSON.stringify(o));
//                             this.challengeConfigs[o.id] = [];
//                             this.challengeConfigs[o.id].push(s);
//                             s.list = r.slice();
//                             s = JSON.parse(JSON.stringify(o));
//                             this.challengeConfigs[o.id].push(s);
//                             s.list = r.slice();
//                         }
//                         return [4, this.jiazai_guanqia_peizhi()];
//                     case 2:
//                         a.sent();
//                         return [4, this.jiazai_tiaozhan_peizhi()];
//                     case 3:
//                         a.sent();
//                         a.label = 4;
//                     case 4:
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.jiazai_tiaozhan_peizhi = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var s;
//             var c;
//             var l;
//             var u;
//             var p;
//             var f;
//             var d;
//             var h;
//             var _;
//             var P;
//             var g;
//             var y;
//             var m;
//             var v;
//             var D;
//             var L;
//             var b;
//             var C;
//             var H;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         return [4, this.levelGenerator.jiazai_tiaozhan_peizhi()];
//                     case 1:
//                         for (e = a.sent(), t = e.level1, n = e.level2, e.rewards, o = 0, i = t; o < i.length; o++) {
//                             c = i[o];
//                             l = c.displace;
//                             u = c.difficult;
//                             p = c.increase;
//                             f = c.reduce;
//                             d = c.volatilityMultiple;
//                             h = c.id;
//                             _ = l.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             P = [Number(_[0]), Number(_[1])];
//                             g = [Number(_[2]), Number(_[3])];
//                             y = [Number(_[4]), Number(_[5])];
//                             m = p.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             v = [Number(m[0]), Number(m[1])];
//                             D = [Number(m[2]), Number(m[3])];
//                             L = f.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             b = [Number(L[0]), Number(L[1])];
//                             C = [Number(L[2]), Number(L[3])];
//                             (H = this.challengeConfigs[h][0]).displaceBase = P;
//                             H.zhixing_cengji_weiyi_caozuo = g;
//                             H.displaceMax = y;
//                             H.increaseBase = v;
//                             H.increaseLayer = D;
//                             H.reduceBase = b;
//                             H.reduceLayer = C;
//                             H.volatilityMultiple = d;
//                             H.difficult = u;
//                             this.challengeConfigs[h][0] = H;
//                         }
//                         for (r = 0, s = n; r < s.length; r++) {
//                             c = s[r];
//                             l = c.displace;
//                             u = c.difficult;
//                             p = c.increase;
//                             f = c.reduce;
//                             d = c.volatilityMultiple;
//                             h = c.id;
//                             _ = l.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             P = [Number(_[0]), Number(_[1])];
//                             g = [Number(_[2]), Number(_[3])];
//                             y = [Number(_[4]), Number(_[5])];
//                             m = p.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             v = [Number(m[0]), Number(m[1])];
//                             D = [Number(m[2]), Number(m[3])];
//                             L = f.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             b = [Number(L[0]), Number(L[1])];
//                             C = [Number(L[2]), Number(L[3])];
//                             (H = this.challengeConfigs[h][1]).displaceBase = P;
//                             H.zhixing_cengji_weiyi_caozuo = g;
//                             H.displaceMax = y;
//                             H.increaseBase = v;
//                             H.increaseLayer = D;
//                             H.reduceBase = b;
//                             H.reduceLayer = C;
//                             H.volatilityMultiple = d;
//                             H.difficult = u;
//                             this.challengeConfigs[h][1] = H;
//                         }
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.jiazai_guanqia_peizhi = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             var n;
//             var o;
//             var i;
//             var r;
//             var s;
//             var c;
//             var l;
//             var p;
//             var f;
//             var d;
//             var h;
//             var _;
//             var P;
//             var g;
//             var y;
//             var m;
//             var v;
//             var D;
//             var L;
//             var b;
//             var C;
//             var H;
//             var I;
//             var S;
//             var w;
//             return __generator(this, function (a) {
//                 switch (a.label) {
//                     case 0:
//                         return [4, this.levelGenerator.jiazai_peizhi()];
//                     case 1:
//                         for (e = a.sent(), t = 0, n = e; t < n.length; t++) {
//                             o = n[t];
//                             i = o.displace;
//                             r = o.difficult;
//                             s = o.increase;
//                             c = o.reduce;
//                             l = o.volatilityMultiple;
//                             p = o.id;
//                             f = i.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             d = [Number(f[0]), Number(f[1])];
//                             h = [Number(f[2]), Number(f[3])];
//                             _ = [Number(f[4]), Number(f[5])];
//                             P = s.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             g = [Number(P[0]), Number(P[1])];
//                             y = [Number(P[2]), Number(P[3])];
//                             m = c.replace(/\(/g, "").replace(/\)/g, "").split(",");
//                             v = [Number(m[0]), Number(m[1])];
//                             D = [Number(m[2]), Number(m[3])];
//                             this.configMap[p].displaceBase = d;
//                             this.configMap[p].zhixing_cengji_weiyi_caozuo = h;
//                             this.configMap[p].displaceMax = _;
//                             this.configMap[p].increaseBase = g;
//                             this.configMap[p].increaseLayer = y;
//                             this.configMap[p].reduceBase = v;
//                             this.configMap[p].reduceLayer = D;
//                             this.configMap[p].volatilityMultiple = l;
//                             this.configMap[p].difficult = r;
//                         }
//                         for (
//                             L = AL_GAME_HUA_GameConst.GameConst.getInstance(), b = L.lvconfs, C = 0, H = b;
//                             C < H.length;
//                             C++
//                         ) {
//                             I = H[C];
//                             S = I[0];
//                             (w = I[1]) &&
//                                 w.length > 0 &&
//                                 (this.customLvConf[S] = {
//                                     displaceBase: [w[0], w[1]],
//                                     zhixing_cengji_weiyi_caozuo: [w[2], w[3]],
//                                     displaceMax: [w[4], w[5]]
//                                 });
//                         }
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.huoqu_wangge_map = function () {
//         return this.configMap;
//     };
//     t.prototype.huoqu_wangge_peizhi_by_id = function (e) {
//         if (
//             AL_GAME_HUA_GameConst.GameConst.getInstance().lv2hard_enabled &&
//             AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() ==
//                 AL_GAME_HUA_GameConst.GameConst.getInstance().lv2hard_level
//         ) {
//             return f;
//         }
//         var t = this.configMap[e];
//         console.log(e, t);
//         var n = AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia();
//         if (n in this.customLvConf) {
//             var o = this.customLvConf[n];
//             var i = o.displaceBase;
//             var r = o.zhixing_cengji_weiyi_caozuo;
//             var a = o.displaceMax;
//             t.displaceBase = i;
//             t.zhixing_cengji_weiyi_caozuo = r;
//             t.displaceMax = a;
//         }
//         return t;
//     };
//     t.prototype.huoqu_tiaozhan_guanqia_peizhi_by_id = function (e, t) {
//         return this.challengeConfigs[e][t - 1];
//     };
//     t.prototype.huoqu_teshu_guanqia_peizhi_by_id = function (e) {
//         return this.configMap[e];
//     };
//     t.prototype.huoqu_guanqia_increase = function (e) {
//         var t = e - 1;
//         var n = Math.max(0, 1.09 * Math.log(t / 100 + 1)) + 1;
//         return Math.min(3, n);
//     };
//     t.prototype.huoqu_nandu_by_guanqia = function (e) {
//         var t = e - 1;
//         return (20 * Math.cos((t * Math.PI) / 5 + Math.PI)) / 2 + 10;
//     };
//     t.prototype.huoqu_displace_jiange = function (e) {
//         var t = m[e];
//         return AL_GAME_HUA_Common.Common.huoqu_weight_suoyin(t) + 1;
//     };
//     t.instance = null;
//     return t;
// })(AL_GAME_HUA_ConfigHandler.ConfigHandler);
// exports.LatticeConfig = v;

// 定义一个全局变量 o
let o;

// 导出 LatticeConfig 变量
exports.LatticeConfig = void 0;

// 导入其他模块
const AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
const AL_GAME_HUA_GameData = require("./AL_GAME_HUA_GameData");
const AL_GAME_HUA_ConfigHandler = require("./AL_GAME_HUA_ConfigHandler");
const AL_GAME_HUA_GameConst = require("./AL_GAME_HUA_GameConst");
const AL_GAME_HUA_LevelConfig = require("./AL_GAME_HUA_LevelConfig");

// 配置对象 f 的定义
const defaultConfig = {
    id: 1006,
    row: 4,
    col: 3,
    list: new Array(12).fill(0),
    difficult: 1,
    displaceBase: [3, 3],
    zhixing_cengji_weiyi_caozuo: [3, 3],
    displaceMax: [50, 50],
    increaseBase: [0.08, 0.08],
    increaseLayer: [0.13, 0.13],
    reduceBase: [0.08, 0.08],
    reduceLayer: [0.13, 0.13],
    volatilityMultiple: 1,
    type: 1
};

// 初始化权重数组
const weightArray1 = [6, 3, 1];
const emptyGridWeights = [0, 0, 1];
const nullGridWeights = [1, 0, 0];
const specialGrid1Weights = [5, 3, 2];
const specialGrid2Weights = [5, 3, 2];

/**
 * LatticeLevelGenerator 类，用于计算和生成关卡配置。
 */
class LatticeLevelGenerator extends AL_GAME_HUA_ConfigHandler.ConfigHandler {
    constructor() {
        super();
        this.configs = [];
    }

    /**
     * 加载 LatticeGenerate 配置的文本文件。
     */
    async jiazai_peizhi() {
        const configData = await this.yibu_jiazai_txt_peizhi("LatticeGenerate");
        if (configData) {
            this.configs = configData;
            return configData;
        }
        return null;
    }

    /**
     * 加载 ChallengeGenerate 配置的文本文件。
     */
    async jiazai_tiaozhan_peizhi() {
        const configData = await this.yibu_jiazai_txt_peizhi("ChallengeGenerate");
        if (configData) {
            this.challengeConfigs = configData;
            return configData;
        }
        return null;
    }

    /**
     * 根据传入的关卡编号，生成关卡配置。
     */
    shengcheng_guanqia_peizhi_by_level_number(levelNumber) {
        let difficulty;
        const weights = weightArray1.slice();
        levelNumber % 9 === 0 ? (difficulty = 3) : (weights.pop(), (difficulty = this.huoqu_weight_suoyin(weights) + 1));
        
        const config = this.configs
            .filter(config => config.difficult === difficulty)
            .huoqu_shuzu_zhong_suiji_yige_yuansu();
        
        return this.shengcheng_guanqia_peizhi_by_level_number_peizhi(levelNumber, config);
    }

    /**
     * 生成挑战配置。
     */
    shengcheng_tiaozhan_guanqia_peizhi_by_level_number(levelNumber) {
        const levelConfigs = this.challengeConfigs;
        const level1 = levelConfigs.level1;
        const level2 = levelConfigs.level2;
        const config1 = level1.huoqu_shuzu_zhong_suiji_yige_yuansu();
        const config2 = level2.huoqu_shuzu_zhong_suiji_yige_yuansu();
        return [this.shengcheng_guanqia_peizhi_by_level_number_peizhi(levelNumber, config1), this.shengcheng_guanqia_peizhi_by_level_number_peizhi(levelNumber, config2)];
    }

    /**
     * 根据现有配置生成关卡。
     */
    shengcheng_guanqia_peizhi_by_level_number_peizhi(levelNumber, config) {
        const {id, latticeTotal, type, latticePer, elementTypeCount, empty, timePer, null0, null1, null2, null3, single, lock, difficult} = config;
        const levelConfig = {};
        levelConfig.ID = levelNumber;
        levelConfig.Lattce = latticeTotal;

        // 获取类型权重和随机数的索引
        const elementTypeWeightsIndex = this.huoqu_weight_suoyin(emptyGridWeights);
        const elementTypeRanges = elementTypeCount.replace(/\(/g, "").replace(/\)/g, "").split(",");
        const elementType = Math.get_random_int(Number(elementTypeRanges[2 * elementTypeWeightsIndex]), Number(elementTypeRanges[2 * elementTypeWeightsIndex + 1]) + 1);
        levelConfig.Type = elementType;
        levelConfig.Number = 3 * elementType;

        // 获取空格权重和随机数的索引
        const emptyWeightsIndex = this.huoqu_weight_suoyin(nullGridWeights);
        const emptyRanges = empty.replace(/\(/g, "").replace(/\)/g, "").split(",");
        const emptyGridCount = emptyWeightsIndex === 0 
            ? Math.get_random_int(Number(emptyRanges[0]), Number(emptyRanges[1]) + 1)
            : Number(emptyRanges[emptyWeightsIndex + 1]);
        levelConfig.Empty = emptyGridCount;

        // 处理 null0 到 null3
        levelConfig.Null0 = this.processNullGrids(null0);
        levelConfig.Null1 = this.processNullGrids(null1);
        levelConfig.Null2 = this.processNullGrids(null2);
        levelConfig.Null3 = this.processNullGrids(null3);

        // 处理 Specialgrid1 和 Specialgrid2
        levelConfig.Specialgrid1 = this.processSpecialGrid(single, specialGrid1Weights);
        levelConfig.Specialgrid2 = this.processSpecialGrid(lock, specialGrid2Weights);

        // 处理 Time
        const timeRanges = timePer.replace(/\(/g, "").replace(/\)/g, "").split(",");
        const time = ((3 * elementType) / latticePer) * Math.get_random_int(Number(timeRanges[2 * emptyWeightsIndex]), Number(timeRanges[2 * emptyWeightsIndex + 1]) + 1);
        levelConfig.Time = Math.round(time);

        // 处理其他字段
        levelConfig.Storey = levelConfig.Specialgrid1 > 0 ? 3 : 0;
        levelConfig.shelf_type = Number(id);
        levelConfig.latticeType = type;
        levelConfig.difficult = difficult;

        return levelConfig;
    }

    /**
     * 处理给定的 nullGrid 字段并返回数组。
     */
    processNullGrids(nullGrid) {
        return typeof nullGrid === "number"
            ? [Number(nullGrid), Number(nullGrid), Number(nullGrid)]
            : [Number(nullGrid.split(",")[0]), Number(nullGrid.split(",")[1]), Number(nullGrid.split(",")[2])];
    }

    /**
     * 根据给定的 specialGrid 字段和权重计算 SpecialGrid。
     */
    processSpecialGrid(specialGrid, weights) {
        if (Number(specialGrid) === 0) {
            return 0;
        }
        const weightIndex = this.huoqu_weight_suoyin(weights);
        const item = specialGrid.split(",");
        return Number(item[weightIndex]);
    }

    /**
     * 生成关卡。
     */
    async generate() {
        const levelConfigs = [];
        const baseConfigData = await AL_GAME_HUA_LevelConfig.LevelConfig.getInstance().jiazai_peizhi();
        const configData = await this.jiazai_peizhi();

        for (let index = 0; index < baseConfigData.length; index++) {
            if (index < 10 || Math.random() > 0.5) {
                levelConfigs.push(baseConfigData[index]);
                continue;
            }

            const baseConfig = baseConfigData[index];
            const {ID, shelf_type, Null0, Null1, Null2, Null3, Lattce} = baseConfig;
            const levelId = shelf_type === 1 ? 1000 + Lattce / 3 : shelf_type;
            const newConfig = this.shengcheng_guanqia_peizhi_by_level_number_peizhi(
                ID,
                configData.filter(config => config.id === levelId)[0]
            );

            newConfig.Null0 = Null0;
            newConfig.Null1 = Null1;
            newConfig.Null2 = Null2;
            newConfig.Null3 = Null3;
            levelConfigs.push(newConfig);
        }
    }

    /**
     * 生成挑战关卡。
     */
    async shengcheng_tiaozhan_guanqia() {
        const challengeConfigs = [];
        for (let levelIndex = 0; levelIndex < 200; levelIndex++) {
            const config = this.shengcheng_tiaozhan_guanqia_peizhi_by_level_number(levelIndex);
            challengeConfigs.push(config);
        }
        console.log(JSON.stringify(challengeConfigs));
    }

    /**
     * 生成特殊配置
     */
    async shengcheng_teshu_guanqia() {
        return;
    }

    /**
     * 计算权重索引。
     */
    huoqu_weight_suoyin(weights) {
        const totalWeight = weights.reduce((acc, weight) => acc + weight, 0);
        const randomValue = Math.random() * totalWeight;

        let accumulatedWeight = 0;
        for (let index = 0; index < weights.length; index++) {
            accumulatedWeight += weights[index];
            if (randomValue < accumulatedWeight) {
                return index;
            }
        }
        return weights.length - 1;
    }
}

// 初始化一个名为 latticeWeights 的二维数组
const latticeWeights = [
    [60, 40, 30, 10, 5],
    [30, 40, 60, 10, 5],
    [10, 30, 60, 10, 5]
];

/**
 * LatticeConfig 类，用于管理和生成关卡配置。
 */
class LatticeConfig extends AL_GAME_HUA_ConfigHandler.ConfigHandler {
    constructor() {
        super();
        this.levelGenerator = new LatticeLevelGenerator();
        this.configMap = {};
        this.challengeConfigs = {};
        this.customLvConf = {};

        const gameConst = AL_GAME_HUA_GameConst.GameConst.getInstance();
        if (gameConst.lv2hard_enabled) {
            const levelConfig = gameConst.lv2confs[1];
            defaultConfig.displaceBase = [levelConfig[0], levelConfig[1]];
            defaultConfig.zhixing_cengji_weiyi_caozuo = [levelConfig[2], levelConfig[3]];
            defaultConfig.displaceMax = [levelConfig[4], levelConfig[5]];
        }
    }

    // 获取 LatticeConfig 的单例。
    static getInstance() {
        if (!this.instance) {
            this.instance = new LatticeConfig();
        }
        return this.instance;
    }

    // 销毁 LatticeConfig 的单例
    destroy() {
        LatticeConfig.instance = null;
    }

    /**
     * 加载配置。
     */
    async jiazai_peizhi() {
        const configData = await this.yibu_jiazai_txt_peizhi("Lattice");
        if (configData) {
            for (let latticeConfig of configData) {
                const latticeList = String(latticeConfig.list)
                    .split("")
                    .map(value => parseInt(value));
                // 逐个加载并存储配置
                this.configMap[latticeConfig.id] = JSON.parse(JSON.stringify(latticeConfig));
                this.configMap[latticeConfig.id].list = latticeList.slice();

                // 逐个加载并存储挑战配置
                let challengeConfigList = JSON.parse(JSON.stringify(latticeConfig));
                this.challengeConfigs[latticeConfig.id] = [];
                this.challengeConfigs[latticeConfig.id].push(challengeConfigList);

                challengeConfigList = JSON.parse(JSON.stringify(latticeConfig));
                this.challengeConfigs[latticeConfig.id].push(challengeConfigList);
            }
            await this.jiazai_guanqia_peizhi();
            await this.jiazai_tiaozhan_peizhi();
        }
    }

    /**
     * 加载挑战配置。
     */
    async jiazai_tiaozhan_peizhi() {
        const challengeData = await this.levelGenerator.jiazai_tiaozhan_peizhi();
        const {level1, level2} = challengeData;

        for (let config of level1) {
            this.updateChallengeConfig(config, 0);
        }

        for (let config of level2) {
            this.updateChallengeConfig(config, 1);
        }
    }

    /**
     * 更新挑战配置。
     */
    updateChallengeConfig(config, challengeIndex) {
        const {displace, difficult, increase, reduce, volatilityMultiple, id} = config;
        const displaceArray = this.convertTextToArray(displace);
        const increaseArray = this.convertTextToArray(increase);
        const reduceArray = this.convertTextToArray(reduce);

        const challengeConfig = this.challengeConfigs[id][challengeIndex];
        challengeConfig.displaceBase = [displaceArray[0], displaceArray[1]];
        challengeConfig.zhixing_cengji_weiyi_caozuo = [displaceArray[2], displaceArray[3]];
        challengeConfig.displaceMax = [displaceArray[4], displaceArray[5]];
        challengeConfig.increaseBase = [increaseArray[0], increaseArray[1]];
        challengeConfig.increaseLayer = [increaseArray[2], increaseArray[3]];
        challengeConfig.reduceBase = [reduceArray[0], reduceArray[1]];
        challengeConfig.reduceLayer = [reduceArray[2], reduceArray[3]];
        challengeConfig.volatilityMultiple = volatilityMultiple;
        challengeConfig.difficult = difficult;
    }

    /**
     * 将文本转换为数值数组。
     */
    convertTextToArray(text) {
        return text.replace(/\(/g, "").replace(/\)/g, "").split(",").map(Number);
    }

    /**
     * 加载关卡配置。
     */
    async jiazai_guanqia_peizhi() {
        const configData = await this.levelGenerator.jiazai_peizhi();
        
        for (let config of configData) {
            this.updateConfigMap(config);
        }

        const gameConst = AL_GAME_HUA_GameConst.GameConst.getInstance();
        const levelConfigs = gameConst.lvconfs;

        for (const levelConfig of levelConfigs) {
            const [levelId, configValues] = levelConfig;
            if (configValues && configValues.length > 0) {
                this.customLvConf[levelId] = {
                    displaceBase: [configValues[0], configValues[1]],
                    zhixing_cengji_weiyi_caozuo: [configValues[2], configValues[3]],
                    displaceMax: [configValues[4], configValues[5]]
                };
            }
        }
    }

    /**
     * 更新 configMap。
     */
    updateConfigMap(config) {
        const {displace, difficult, increase, reduce, volatilityMultiple, id} = config;
        const displaceArray = this.convertTextToArray(displace);
        const increaseArray = this.convertTextToArray(increase);
        const reduceArray = this.convertTextToArray(reduce);

        const currentConfig = this.configMap[id];
        currentConfig.displaceBase = [displaceArray[0], displaceArray[1]];
        currentConfig.zhixing_cengji_weiyi_caozuo = [displaceArray[2], displaceArray[3]];
        currentConfig.displaceMax = [displaceArray[4], displaceArray[5]];
        currentConfig.increaseBase = [increaseArray[0], increaseArray[1]];
        currentConfig.increaseLayer = [increaseArray[2], increaseArray[3]];
        currentConfig.reduceBase = [reduceArray[0], reduceArray[1]];
        currentConfig.reduceLayer = [reduceArray[2], reduceArray[3]];
        currentConfig.volatilityMultiple = volatilityMultiple;
        currentConfig.difficult = difficult;
    }

    /**
     * 获取 map_lattice_list 配置。
     */
    huoqu_wangge_map() {
        return this.configMap;
    }

    /**
     * 根据 ID 获取 latticeConfig。
     */
    huoqu_wangge_peizhi_by_id(id) {
        if (
            AL_GAME_HUA_GameConst.GameConst.getInstance().lv2hard_enabled &&
            AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia() === AL_GAME_HUA_GameConst.GameConst.getInstance().lv2hard_level
        ) {
            return defaultConfig;
        }

        const config = this.configMap[id];
        const unlockedLevel = AL_GAME_HUA_GameData.GameData.getInstance().huoqu_yijiesuo_guanqia();

        if (unlockedLevel in this.customLvConf) {
            const customConfig = this.customLvConf[unlockedLevel];
            config.displaceBase = customConfig.displaceBase;
            config.zhixing_cengji_weiyi_caozuo = customConfig.zhixing_cengji_weiyi_caozuo;
            config.displaceMax = customConfig.displaceMax;
        }

        return config;
    }

    /**
     * 根据 ID 和挑战序号获取挑战配置。
     */
    huoqu_tiaozhan_guanqia_peizhi_by_id(id, challengeIndex) {
        return this.challengeConfigs[id][challengeIndex - 1];
    }

    /**
     * 根据 ID 获取特殊配置。
     */
    huoqu_teshu_guanqia_peizhi_by_id(id) {
        return this.configMap[id];
    }

    /**
     * 根据等级获取 levelIncrease。
     */
    huoqu_guanqia_increase(level) {
        const levelIndex = level - 1;
        let increase = Math.max(0, 1.09 * Math.log(levelIndex / 100 + 1)) + 1;
        return Math.min(3, increase);
    }

    /**
     * 根据等级获取 difficult。
     */
    huoqu_nandu_by_guanqia(level) {
        const levelIndex = level - 1;
        return (20 * Math.cos((levelIndex * Math.PI) / 5 + Math.PI)) / 2 + 10;
    }

    /**
     * 根据给定的 index 获取 displaceInterval。
     */
    huoqu_displace_jiange(index) {
        const weights = latticeWeights[index];
        return AL_GAME_HUA_Common.Common.huoqu_weight_suoyin(weights) + 1;
    }
}

// 将 LatticeConfig 注册到导出对象
exports.LatticeConfig = LatticeConfig;
